package cn.itcast.web.action;

import cn.itcast.domain.system.Menu;
import cn.itcast.domain.system.User;
import cn.itcast.service.MenuService;
import cn.itcast.web.action.common.BaseAction;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Actions
public class MenuAction extends BaseAction<Menu> {
    @Autowired
    private MenuService menuService;

    //    url : '../../menu_list.action',
    @Action(value = "menu_list", results = {@Result(name = "success", type = "json")})
    public String findAll() {
        List<Menu> menus = menuService.findAll();
        ServletActionContext.getContext().getValueStack().push(menus);
        return SUCCESS;
    }

    //    action="../../menu_save.action">
    @Action(value = "menu_save", results = {@Result(name = "success", location = "/pages/system/menu.html", type = "redirect")})
    public String save() {
        menuService.save(model);
        return SUCCESS;
    }

    //$.post("./menu_showmenu.action" 根据当前用户权限查询该用户menu菜单
    @Action(value = "menu_showmenu", results = {
            @Result(name = "success", type = "json")})
    public String showMenu() {
        //从subject 中获取user对象
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Menu>menus= menuService.findByUser(user);
        ServletActionContext.getContext().getValueStack().push(menus);
        return SUCCESS;
    }
}
